package com.xinqi.modules.discuss.notice.controller.app;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.discuss.notice.dto.req.*;
import com.xinqi.modules.discuss.notice.dto.rsp.CourseNoticeResultDTO;
import com.xinqi.modules.discuss.notice.dto.rsp.CourseNoticeStudentLogResultDTO;
import com.xinqi.modules.discuss.notice.service.CourseNoticeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * APP -- 课程公告
 *
 * @author: yyzhen
 * @date: 2023/03/21
 */
@Tag(name = "App-课程公告", description = "课程公告")
@RestController
@RequestMapping("/app/notice")
@RequiredArgsConstructor
public class CourseNoticeAppController {
    private final CourseNoticeService courseNoticeService;

    /**
     * 分页查询
     *
     * @Title:
     * @MethodName: page
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.discuss.notice.dto.rsp.CourseNoticeResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:48
     */
    @GetMapping("/page")
    public R<PageInfo<CourseNoticeResultDTO>> page(Param pageable, CourseNoticeQueryDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(courseNoticeService.selectPage(pageable, dto));
    }

    /**
     * 公告详情
     *
     * @Title:
     * @MethodName: info
     * [id]
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.discuss.notice.dto.rsp.CourseNoticeResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:48
     */
    @GetMapping("/info/{id}")
    public R<CourseNoticeResultDTO> info(@PathVariable("id") Long id) {
        return R.ok(courseNoticeService.findByIdAndUserId(id, LoginHelper.getUserId()));
    }

    /**
     * 添加公告
     *
     * @Title:
     * @MethodName: insert
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:47
     */
    @PostMapping("/add")
    public R<Void> insert(@Validated @RequestBody CourseNoticeCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        boolean flag = courseNoticeService.insert(dto);
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease())) {
            if (flag) {
                return R.ok(MessageUtils.message("course.release.message.success"));
            } else {
                return R.fail(MessageUtils.message("course.release.message.fail"));
            }
        } else {
            if (flag) {
                return R.ok(MessageUtils.message("course.notice.add.success"));
            } else {
                return R.fail(MessageUtils.message("course.notice.add.fail"));
            }
        }
    }

    /**
     * 编辑公告
     *
     * @Title:
     * @MethodName: update
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:47
     */
    @PostMapping("/edit")
    public R<Void> update(@Validated @RequestBody CourseNoticeUpdateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(courseNoticeService.updateById(dto));
    }

    /**
     * 删除公告
     *
     * @Title:
     * @MethodName: remove
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:47
     */
    @PostMapping("/remove")
    public R<Void> remove(@RequestBody CourseNoticeDeleteDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (courseNoticeService.remove(dto)) {
            return R.ok(MessageUtils.message("course.notice.remove.success"));
        }
        return R.fail(MessageUtils.message("course.notice.remove.fail"));
    }

    /**
     * 批量删除
     *
     * @Title:
     * @MethodName: batchRemove
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 15:47
     */
    @PostMapping("/batchRemove")
    public R<Void> batchRemove(@RequestBody CourseNoticeDeleteDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (courseNoticeService.batchRemove(dto)) {
            return R.ok(MessageUtils.message("course.notice.remove.success"));
        }
        return R.fail(MessageUtils.message("course.notice.remove.fail"));
    }

    /**
     * 设为置顶
     *
     * @Title:
     * @MethodName: isTop
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 11:47
     */
    @GetMapping("/isTop/{id}")
    public R<Void> isTop(@PathVariable("id") Long id) {
        if (courseNoticeService.isTop(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.notice.message.success"));
        }
        return R.fail(MessageUtils.message("course.notice.message.fail"));
    }

    /**
     * 取消置顶
     *
     * @Title:
     * @MethodName: cancelTop
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 11:47
     */
    @GetMapping("/cancelTop/{id}")
    public R<Void> cancelTop(@PathVariable("id") Long id) {
        if (courseNoticeService.cancelTop(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.notice.message.success"));
        }
        return R.fail(MessageUtils.message("course.notice.message.fail"));
    }

    /**
     * 发布
     *
     * @Title:
     * @MethodName: isRelease
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 11:47
     */
    @GetMapping("/isRelease/{id}")
    public R<Void> isRelease(@PathVariable("id") Long id) {
        if (courseNoticeService.isRelease(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        }
        return R.fail(MessageUtils.message("course.release.message.fail"));
    }

    /**
     * 发布V2版本
     *
     * @Title:
     * @MethodName: isRelease
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 11:47
     */
    @GetMapping("/isRelease/{id}/v2")
    public R<Void> isReleaseV2(@PathVariable("id") Long id) {
        if (courseNoticeService.isReleaseV2(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        }
        return R.fail(MessageUtils.message("course.release.message.fail"));
    }

    /**
     * 确认
     *
     * @Title:
     * @MethodName: confirm
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 16:17
     */
    @GetMapping("/confirm/{id}")
    public R<Void> confirm(@PathVariable("id") Long id) {
        if (courseNoticeService.confirm(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.notice.message.success"));
        }
        return R.fail(MessageUtils.message("course.notice.message.fail"));
    }

    /**
     * 公告下载
     *
     * @Title:
     * @MethodName: download
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 13:46
     */
    @PostMapping("/download")
    public R<List<String>> download(@RequestBody CourseNoticeDownloadDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        List<String> urls = courseNoticeService.download(dto);
        if (CollectionUtils.isEmpty(urls)) {
            return R.fail(MessageUtils.message("course.notice.download.blank"));
        }
        return R.ok(urls);
    }

    /**
     * 已读\确认列表
     *
     * @Title:
     * @MethodName: studentPage
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.discuss.notice.dto.rsp.CourseNoticeStudentLogResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 14:08
     */
    @GetMapping("/studentPage")
    public R<PageInfo<CourseNoticeStudentLogResultDTO>> studentPage(Param pageable, CourseNoticeStudentLogQueryDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(courseNoticeService.selectStudentLogPage(pageable, dto));
    }

    /**
     * 一键提醒
     *
     * @Title:
     * @MethodName: reminder
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 14:35
     */
    @GetMapping("/reminder/{id}")
    public R<Void> reminder(@PathVariable("id") Long id) {
        courseNoticeService.reminderStudent(id, LoginHelper.getUserId());
        return R.ok(MessageUtils.message("course.notice.reminder.success"));
    }

    /**
     * 已读
     *
     * @Title:
     * @MethodName: read
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/22 11:47
     */
    @GetMapping("/read/{id}")
    public R<Void> read(@PathVariable("id") Long id) {
        return R.ok(MessageUtils.message("course.notice.message.success"));
    }

}
